package 链表;

import java.util.Arrays;
import java.util.List;
import java.util.Stack;

/**
 * https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
 * <p>
 * 输入：head = [1,3,2]
 * 输出：[2,3,1]
 */
public class 剑指_Offer_06_从尾到头打印链表 {

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(1, new ListNode(2, new ListNode(3, null)));
        System.out.println(Arrays.toString(reversePrint(listNode1)));
    }

    public static int[] reversePrint(ListNode head) {
        if (head == null) {
            return null;
        }
        Stack<ListNode> listNodes = new Stack<>();

        while (head != null) {
            listNodes.add(head);
            head = head.next;
        }
        int size = listNodes.size();
        int[] result = new int[size];
        for (int i = 0; i < size; i++) {
            result[i] = listNodes.pop().val;
        }

        return result;
    }

    public static ListNode reverseNode(ListNode head) {

        if (head.next == null) return head;

        ListNode curHead = reverseNode(head.next);
        head.next.next = head;
        head.next = null;
        return curHead;
    }

}
